import dayjs from 'dayjs'
import Schema from 'validate'
const formInfo = new Schema({
  houseId:{
    type:String,
    required:true,
    message:'亲填写房屋信息'
  },
  name:{
    type:String,
    required:true,
    message:'亲填写姓名信息'
  },
  gender:{
    type:Number,
    required:true,
    message:'亲填写性别信息'
  },
  mobile:{
    type:String,
    required:true,
    message:'亲填写手机号信息'
  },
  visitDate:{
    type:String,
    required:true,
    message:'亲填写日期信息'
  },
})
Page({
  data: {
    dateLayerVisible: false,
    houseLayerVisible: false,
    houseList: [],
    houseId:'',
    houseInfo:'',
    currentDate: +dayjs(),
    maxDate:+dayjs() + 1000*60*60*24*3,
    minDate:+dayjs(),
    visitDate:'',
    gender:'',
    mobile:'',
    name:''
  },
  onLoad(){
    this.getHouseList()
  },
  // 点击确认时间
  onConfirmDate(e){
    this.setData({
      currentDate:e.detail,
      visitDate:dayjs(e.detail).format('YYYY-MM-DD'),
      dateLayerVisible:false
    })
  },
  // 获取房屋列表
  async getHouseList(){
     const res = await wx.http({url:'/house'})
     this.setData({houseList:res.data})
  },
  // 下拉房屋
  onHouseSelect(e){
     const {id,name} = e.detail
     this.setData({houseId:id,houseInfo:name})
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  goPassport() {
    wx.reLaunch({
      url: '/visitor_pkg/pages/passport/index',
    })
  },
  async addForm(){
    const {houseId,name,gender,mobile,visitDate} = this.data
    const data = {houseId,name,gender,mobile,visitDate}
    const error = formInfo.validate(data);
    if(error.length){
      return wx.utils.toast(errors[0].message)
    }
    await wx.http({url:'/visitor',method:'post',data})
    wx.showToast({
      title: '添加成功',
    })
    setTimeout(()=>{
      wx.navigateTo({
        url: '/visitor_pkg/pages/list/index',
      })
    },1000)
  },
})
